<template>
  <div class="setTest">
    <head-top></head-top>
    <h1 class="title">试卷发布</h1>
    <el-table
      ref="multipleTable"
      v-if="tableData"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 90%; margin: 0 5%"
      :default-sort="{ prop: 'id', order: 'ascending' }"
      @selection-change="handleSelectionChange"
      max-height="calc(100% - 240px)"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="题号" sortable width="120"> </el-table-column>
      <el-table-column prop="title" label="题目" width="320"> </el-table-column>
      <el-table-column prop="itemA" label="选项A" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="itemB" label="选项B" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="itemC" label="选项C" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="itemD" label="选项D" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="answer" label="答案" show-overflow-tooltip>
      </el-table-column>
    </el-table>
    <el-button
      style="display: block; margin: 20px auto"
      type="success"
      @click="releaseTest"
      >发布考试</el-button
    >
    <el-dialog title="考试详情" :visible.sync="dialogFormVisible" center>
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="考试科目" :label-width="formLabelWidth" prop="subject">
          <el-input v-model="form.subject"></el-input>
        </el-form-item>
        <el-form-item label="考试类型" :label-width="formLabelWidth" prop="test_type">
          <el-select v-model="form.test_type" placeholder="请选择考试类型">
            <el-option label="期中考试" value="期中考试"></el-option>
            <el-option label="期末考试" value="期末考试"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="footerbtn">
          <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
          <el-button @click="dialogFormVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import headTop from "@/components/headTop";
import { getTestData, submitTest } from "@/api/getData";
import { baseUrl, baseImgPath } from "@/config/env";
export default {
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      dialogFormVisible: false,
      form: {
        subject: "",
        test_type: "",
      },
      idArr: [],
      formLabelWidth: "120px",
      rules: {
        subject: [{ required: true, message: "请输入考试科目", trigger: "blur" }],
        test_type: [{ required: true, message: "请选择考试类型", trigger: "blur" }],
      },
    };
  },
  components: {
    headTop,
  },
  mounted() {},
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    releaseTest() {
      if (this.multipleSelection.length != 0) {
        this.multipleSelection.map((item) => {
          this.idArr.push(item.id);
        });
        this.dialogFormVisible = true;
      } else {
        this.$message({
          type: "warning",
          message: "请选择题目后再发布考试！！！",
        });
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = new URLSearchParams();
          params.append("subject", this.form.subject);
          params.append("idArr", this.idArr.toString());
          params.append("test_type", this.form.test_type);

          console.log(params);
          submitTest(params)
            .then((res) => {
              let { code, msg } = res.data;
              if (code == 200) {
                this.$message({
                  type: "success",
                  message: msg,
                });
                this.dialogFormVisible = false;
              } else {
                this.$message({
                  type: "error",
                  message: msg,
                });
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  created() {
    getTestData()
      .then((res) => {
        console.log(res);
        let { code, msg, data2 } = res.data;
        if (code == 200) {
          this.tableData = data2;
        } else {
          this.$message({
            type: "error",
            message: msg,
          });
        }
      })
      .catch((err) => {
        console.log(err);
      });
  },
};
</script>

<style lang="less">
@import "../style/mixin";
.setTest {
  .title {
    text-align: center;
    color: #20a0ff;
    margin: 30px 0;
  }
  .button_submit {
    text-align: center;
  }
  .el-input {
    width: 200px;
  }
  .footerbtn {
    text-align: center;
  }
  .el-dialog--small {
    width: 500px;
  }
}
</style>
